package com.example.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.model.Song;
import com.example.service.SongService;
import com.example.utils.ResultVOUtil;

@RestController
@RequestMapping("/api/song")
public class SongController {

	@Autowired
	private SongService songService;

	@GetMapping("/list")
	public Object list() {
		List<Song> songList = songService.findAll();
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("songList", songList);
		return ResultVOUtil.success(resultMap);
	}

	@GetMapping("/listByParams")
	public Object listByParams(@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
			@RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {

		Song song = new Song();
		song.setName(name);

		return ResultVOUtil.success(songService.querySongListPaged(song, currentPage, limit));
	}

	// // 添加或修改
	@PostMapping("/add")
	public Object add(Song song) {
		System.out.println("保存歌名:" + song.getName());
		System.out.println("保存歌手:" + song.getSinger());

		song.setCreatetime(new Date().getTime());
		song.setUpdatetime(new Date().getTime());
		songService.save(song);
		return ResultVOUtil.success();
	}

	@PostMapping("/update")
	public Object update(Song song) {
		System.out.println("修改歌名:" + song.getName());
		System.out.println("修改歌手:" + song.getSinger());

		song.setUpdatetime(new Date().getTime());
		songService.update(song);
		return ResultVOUtil.success();
	}

	// 删除
	@PostMapping("/delete")
	public Object delete(@RequestParam("id") int id) {
		System.out.println("删除歌曲的id: " + id);
		songService.delete(id);
		return ResultVOUtil.success();
	}

	// 根据id获取详细
	@GetMapping("/getDetail")
	public Object getDetail(@RequestParam("id") int id) {
		Song song = songService.getDetailById(id);
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("song", song);
		return ResultVOUtil.success(resultMap);
	}

	// 根据歌曲名称查询
	@GetMapping("/getSongListByName")
	public Object getSongListByName(@RequestParam("name") String name) {
		List<Song> songList = songService.querySongListByName(name);
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("songList", songList);
		return ResultVOUtil.success(resultMap);
	}

	// 根据歌曲名称查询
	@GetMapping("/saveSongTransactional")
	public Object saveSongTransactional() {
		Song song = new Song();
		song.setName("夜曲");
		song.setSinger("周杰伦");
		song.setCreatetime(new Date().getTime());
		song.setUpdatetime(new Date().getTime());
		
		songService.saveSongTransactional(song);
		return ResultVOUtil.success();
	}

}
